﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using YouthCMS.Data;

namespace YouthCMS
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] == null)
            {

                Response.Write("Hvis du vil tilmeldes, så log ind");
            }
            FillNews();
        }

        public void FillNews()
        {

            var list = CreateGroupListNews();
            if (list.Count > 0)
            {
                foreach (var val in list)
                {
                    foreach (var val2 in val)
                    {
                        if (Literal1 != null)
                        {


                            Literal1.Text += "<div class='about'>";
                            Literal1.Text += "<h2 class='title'   id='h2title'>" + val2.Name + "</h2>";
                            Literal1.Text += "<div class='styleForSubmitButtons'>Emne kode:" + val2.SubjectCode;
                            Literal1.Text += "</div> ";
                            foreach (var @teacher in val2.teacherProp)
                            {
                                Literal1.Text += "<div class='styleForSubmitButtons'>Lærer:" + teacher.FirstName +
                                                 teacher.LastName;
                                Literal1.Text += "</div> ";
                            }

                            Literal1.Text += "<div id='entryID' style='clear: both;'  class='entry'>";

                         
                            //for (int i = 0; i < count; i++)
                            //    Console.WriteLine("Value: " + bytes[i]);

                            for (int i = 0; i < val2.Description.Count(); i++ )
                            {
                                Literal1.Text += val2.Description[i];
                            }
                            Literal1.Text += "</div>";
                            Literal1.Text += "</div>";

                        }
                    }
                }
            }
            else
            {
                Literal1.Text += "<h2 class='title' runat='server'  id='h2title'>" + "Gruppe Fejl" + "</h2>";
                Literal1.Text += "<div class='styleForSubmitButtons'>" + "Fejlkode 9";
                Literal1.Text += "</div> ";
                Literal1.Text += "<div id='entryID'  runat='server' class='entry'>";
                Literal1.Text += "Der er desværre ingen grupper, men du kan logge ind og finde formularen under kontrolpanelet";

                Literal1.Text += "</div>";
            }
        }

        public DataSet MoveList()
        {
            var dataBase = new Database();

            var ds = dataBase.SelectAllGroups();


            return ds;
        }

        public List<List<Group>> CreateGroupListNews()
        {


            var ds = MoveList();
            var dt = ds.Tables["Group"];
            var groupOfGroups = new List<Group>();
            //return (from DataRow row in dt.Rows select new Group().Add(new Teacher{FirstName = })
            //{

            //})
            Teacher person = null;
            foreach (DataRow dt3 in dt.Rows)
            {
                person = new Teacher() { FirstName = dt3["FirstName"].ToString(), LastName = dt3["LastName"].ToString() };
                // dt3.Rows.Add(item["Eid"], item["Dev"], item["Test"], item["Prod"]);
            }

            foreach (DataRow dt23 in dt.Rows)
            {

                var group = new Group
                                  {
                                      Name = dt23["name"].ToString(),
                                      StartTime = DateTime.Parse(dt23["StartTime"].ToString()),
                                      SubjectCode = dt23["SubjectCode"].ToString(),
                                      Description = dt23["description"].ToString(),
                                  };
                group.Add(person);

                groupOfGroups.Add(group);

            }

            var grList = new List<List<Group>> { groupOfGroups };
            return grList;
            //var list = (from Group dt2 in dt.Rows select dt2.Add(person)).ToList();
            //return list;

        }
    }
}
